//
//  JgAdsBannerAdapter.m
//  AnyThinkSDKDemo
//
//  Created by Erik on 2021/12/8.
//  Copyright © 2021 抽筋的灯. All rights reserved.
//

#import "JgAdsBannerAdapter.h"
#import <JgAdsSDK/JgAdsSDK.h>
#import "JgAdsBannerCustomEvent.h"

@interface JgAdsBannerAdapter ()

@property(nonatomic, readonly) JgAdsBannerAdView *bannerView;
@property(nonatomic, readonly) JgAdsBannerCustomEvent *customEvent;
@end

@implementation JgAdsBannerAdapter

-(instancetype) initWithNetworkCustomInfo:(NSDictionary*)serverInfo localInfo:(NSDictionary*)localInfo {
    self = [super init];
    if (self != nil) {
        [JgAdsSDK initWithAppId:serverInfo[@"app_id"] completionBlock:^(NSError * _Nullable error) {
            if(error){
                NSLog(@"JgAdsSDK init failed：%@",error);
            }
        }];
    }
    return self;
}


-(void) loadADWithInfo:(NSDictionary*)serverInfo localInfo:(NSDictionary*)localInfo completion:(void (^)(NSArray<NSDictionary *> *, NSError *))completion {
    
    NSDictionary *extraInfo = localInfo;
    CGSize adSize = [extraInfo[kATAdLoadingExtraBannerAdSizeKey] respondsToSelector:@selector(CGSizeValue)] ? [extraInfo[kATAdLoadingExtraBannerAdSizeKey] CGSizeValue] : CGSizeMake(320.0f, 50.0f);

    _customEvent = [[JgAdsBannerCustomEvent alloc] initWithInfo:serverInfo localInfo:localInfo];
    _customEvent.requestCompletionBlock = completion;
    dispatch_async(dispatch_get_main_queue(), ^{
        self->_bannerView = [[JgAdsBannerAdView alloc] initWithFrame:CGRectMake(.0f, .0f, adSize.width, adSize.height) posId:serverInfo[@"slot_id"]];
        self->_bannerView.viewController = [ATBannerCustomEvent rootViewControllerWithPlacementID:((ATPlacementModel*)serverInfo[kATAdapterCustomInfoPlacementModelKey]).placementID requestID:serverInfo[kATAdapterCustomInfoRequestIDKey]];
        self->_bannerView.delegate = self->_customEvent;
        [self->_bannerView loadRequest];
    });
}


@end
